<?php

/**
 * @example Tela de controler
 * @package SweetPHP
 * @package Apps/Controllers/Examples
 */
class Wellcome extends Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        //$this->edit(1);
        $user = new User();
        if ($user->exists()) {
            $user->get();
        }
        //$this->save(); 
        $this->listUsers(); 
        Routers::changePage('wellcome_view', array('title' => 'Wellcome Sweet PHP', 'user' => $user));
    }

    public function listUsers() {
        $em = new EntityManager();
        $query = new Query();
        $query->statement = $em->createNativeQuery("SELECT * FROM User");
        $list = $query->getResultList();
        $listUser = array();
        for ($i = 0; $i < sizeof($list); $i++) {
            $id = $list[$i][0];
            $listUser[] = $em->find(new User(), $id);
        }
    }

    public function save() {
        $em = new EntityManager();
        $user = new User();
        $user->setLogin("Test");
        $user->setPassword(md5(date('d-m-y')));
        $user->setMail("paul@mail.com");
        $user->getPeople()->setName("Paul Simpson");
        $em->beginTransaction();
        if ($em->persist($user)) {
            $em->commit();
            return true;
        } else {
            $em->rollBack();
            return false;
        }
    }

    public function update() {
        $em = new EntityManager();
        $user = new User();
        $user = $em->find(new User(), 1);
        $user->setLogin("Test");
        $user->setPassword(md5(date('d-m-y')));
        $user->setMail("paul@mail.com");
        $user->getPeople()->setName("Paulo Simpson");
        $em->beginTransaction();
        if ($em->merge($user)) {
            $em->commit();
            return true;
        } else {
            $em->rollBack();
            return false;
        }
    }

    public function delete() {
        $em = new EntityManager();
        $user = new User();
        $user = $em->find(new User(), 8);
        $em->beginTransaction();
        if ($em->remove($user)) {
            $em->commit();
            return true;
        } else {
            $em->rollBack();
            return false;
        }
    }

    public function edit($id = 0) {
        $em = new EntityManager();
        $user = new User();
        $user = $em->find(new User(), $id);
        $user->put();
    }

    public function contact() {
        $e = new EntityManager();
        $user = new User();
        $user->setId(1);
        if (!$user->exists()) {
            $user->post();
            $user->put();
        } else {
            $user->put();
            $user->remove();
        }
        $user->setPeople(null);
        //Session::put($user);
        // Helpers::redirect('welcome', true);
        if (!Session::exists('user')) {
            if ($user != Session::get('user')) {
                Session::put('user', $user);
            }
        }
        return "Wellcome {$user->login}";
    }

    public function redirect() {
        return Helpers::redirect(array('url' => 'wellcome/page', 'disabled' => true, 'time' => 5000, 'ajax' => 'true'));
    }

    public function backIndex() {
        return Helpers::redirect(array('url' => 'wellcome', 'ajax' => 'true'));
    }

    public function page() {
        Routers::changePage('page_view', array('title' => 'Page Test'));
    }

    public function listener() {
        $_SESSION['datetime'] = date('y');
        $ano = $_SESSION['datetime'];
    }

}
